package com.raysdata.smartcity.tools;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Types;

public class GeneratorUtil {
    //根据sql类型数值返回对应Java数据类型
    public static String getTypeByCode(int code){
        String str = "String";
        if(code == Types.INTEGER){
            str = "Integer";
        }
        return str;
    }

    //velocity生成文件到指定path
    public static void merge(Template template, VelocityContext ctx, String path) {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(path);
            template.merge(ctx, writer);
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
    }

    //获得指定生成对象的所在目录
    public static String getPath(String pkg){
        String path = System.getProperty("user.dir");
        if(pkg.equals("dto")){
            //path += File.separator + "src" + File.separator + "k" + File.separator + "dto" + File.separator;
            path += File.separator + "src" + File.separator + "com"+ File.separator +"raysdata"+ File.separator +"smartcity" + File.separator + "dto" + File.separator;
        }
        if(pkg.equals("dao")){
            path += File.separator + "src" + File.separator + "com"+ File.separator +"raysdata"+ File.separator +"smartcity" + File.separator + "dao" + File.separator;
        }
        if(pkg.equals("controller")){
            path += File.separator + "src" + File.separator + "k" + File.separator + "controller" + File.separator;
        }
        return path;
    }

    //字符串首字母大写
    public static String toUpperCaseFirstOne(String name) {
        char[] ch = name.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (i == 0) {
                ch[0] = Character.toUpperCase(ch[0]);
            } else {
                ch[i] = Character.toLowerCase(ch[i]);
            }
        }
        StringBuilder a = new StringBuilder();
        a.append(ch);
        return GeneratorUtil.replaseName(a.toString());
    }

    //字符串首字母大写
    public static String toUpperCaseFirst(String name) {
        char[] ch = name.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (i == 0) {
                ch[0] = Character.toUpperCase(ch[0]);
            } else {
                ch[i] = Character.toLowerCase(ch[i]);
            }
        }
        StringBuilder a = new StringBuilder();
        a.append(ch);
        return a.toString();
    }

    public static String replaseName(String name){
        return  name.replaceAll("_a","A").replaceAll("_b","B").replaceAll("_c","C").replaceAll("_d","D").replaceAll("_e","E").
                 replaceAll("_f","F").replaceAll("_g","G").replaceAll("_h","H").replaceAll("_i","I").replaceAll("_j","J")
                .replaceAll("_k","K").replaceAll("_l","L").replaceAll("_m","M").replaceAll("_n","N").replaceAll("_o","O")
                .replaceAll("_p","P").replaceAll("_q","Q").replaceAll("_r","R").replaceAll("_s","S").replaceAll("_t","T")
                .replaceAll("_u","U").replaceAll("_v","V").replaceAll("_w","W").replaceAll("_x","X").replaceAll("_y","Y")
                .replaceAll("_z","Z");
    }


    public static String toLowerCaseFirstOne(String name) {
        char[] ch = name.toCharArray();
        for (int i = 0; i < ch.length; i++) {
                ch[i] = Character.toLowerCase(ch[i]);
        }
        StringBuilder a = new StringBuilder();
        a.append(ch);
        return GeneratorUtil.replaseName(a.toString());
    }
}
